package cn.edu.cjw.APMFP.ProductionTask.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Util.HBaseUtill;
import cn.edu.cjw.APMFP.Util.HiveUtill;
import cn.edu.cjw.APMFP.Util.StringBackToMap;

public class TaskDAOImpl implements TaskDAO {

	public boolean addAndUpdateTask(ProductionTask task) throws Exception {

		Boolean flag = false;
		Put put = null;

		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(ProductionTask.TASK_TABLE_NAME));

		if (task.getExamine()) {
			put = HBaseUtill.putTaskExamine(task);

		} else {
			put = HBaseUtill.putTaskNotExamine(task);

		}

		table.put(put);

		HBaseUtill.Close(table, conn);
		flag = true;

		return flag;
	}

	public boolean removeTask(ProductionTask task) throws Exception {

		Boolean flag = false;

		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(ProductionTask.TASK_TABLE_NAME));

		Delete delete = new Delete(Bytes.toBytes(task.getTaskId()));

		table.delete(delete);

		HBaseUtill.Close(table, conn);
		flag = true;

		return flag;
	}

	public ProductionTask searchTaskById(String id) throws Exception {

		ProductionTask productionTask = new ProductionTask();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_task_table WHERE TaskId=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, id);

		ResultSet rSet = ps.executeQuery();

		while (rSet.next()) {

			productionTask.setTaskId(rSet.getString("TaskId"));
			productionTask.setFounder(rSet.getString("founder"));
			productionTask.setCreationTime(rSet.getDate("creationTime"));
			productionTask.setChargeMan(rSet.getString("chargeMan"));
			productionTask.setCompletionTime(rSet.getDate("completionTime"));
			productionTask.setDescribe(rSet.getString("taskdescribe"));
			if (Boolean.valueOf(rSet.getString("examine"))) {
				productionTask.setExamine(StringBackToMap.KStringVBool(rSet.getString("percentage")),
						StringBackToMap.KStringVLong(rSet.getString("product")),
						StringBackToMap.KStringVLong(rSet.getString("material")));

			}

		}

		HiveUtill.Close(rSet, ps, conn);

		return productionTask;
	}

	public ArrayList<ProductionTask> searchTaskByIng() throws Exception {
		ArrayList<ProductionTask> list = new ArrayList<ProductionTask>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_task_table WHERE examine=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, "true");

		ResultSet rSet = ps.executeQuery();

		while (rSet.next()) {

			HashMap<String, Boolean> percent = StringBackToMap.KStringVBool(rSet.getString("percentage"));
			Set<Entry<String, Boolean>> entrySet = percent.entrySet();
			for (Entry<String, Boolean> entry : entrySet) {

				if (entry.getValue()) {

				} else {
					ProductionTask productionTask = new ProductionTask(rSet.getString("TaskId"),
							rSet.getString("founder"), rSet.getDate("creationTime"), rSet.getString("chargeMan"),
							rSet.getDate("completionTime"), rSet.getString("taskdescribe"));
					if (Boolean.valueOf(rSet.getString("examine"))) {
						productionTask.setExamine(StringBackToMap.KStringVBool(rSet.getString("percentage")),
								StringBackToMap.KStringVLong(rSet.getString("product")),
								StringBackToMap.KStringVLong(rSet.getString("material")));

					}

					list.add(productionTask);
					break;
				}

			}

		}

		HiveUtill.Close(rSet, ps, conn);

		return list;
	}

	public ArrayList<ProductionTask> searchTaskByExamine() throws Exception {
		ArrayList<ProductionTask> list = new ArrayList<ProductionTask>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_task_table WHERE examine=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, "false");

		ResultSet rSet = ps.executeQuery();

		while (rSet.next()) {

			ProductionTask productionTask = new ProductionTask(rSet.getString("TaskId"), rSet.getString("founder"),
					rSet.getDate("creationTime"), rSet.getString("chargeMan"), rSet.getDate("completionTime"),
					rSet.getString("taskdescribe"));

			list.add(productionTask);

		}

		HiveUtill.Close(rSet, ps, conn);

		return list;
	}

	public ArrayList<ProductionTask> searchTaskComplete() throws Exception {
		ArrayList<ProductionTask> list = new ArrayList<ProductionTask>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_task_table WHERE examine=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, "true");

		ResultSet rSet = ps.executeQuery();

		while (rSet.next()) {

			Boolean flag = false;

			HashMap<String, Boolean> percent = StringBackToMap.KStringVBool(rSet.getString("percentage"));
			Set<Entry<String, Boolean>> entrySet = percent.entrySet();
			for (Entry<String, Boolean> entry : entrySet) {

				if (entry.getValue()) {
					flag = true;
				} else {
					flag = false;
					break;
				}

			}

			if (flag) {
				ProductionTask productionTask = new ProductionTask(rSet.getString("TaskId"), rSet.getString("founder"),
						rSet.getDate("creationTime"), rSet.getString("chargeMan"), rSet.getDate("completionTime"),
						rSet.getString("taskdescribe"));

				productionTask.setExamine(StringBackToMap.KStringVBool(rSet.getString("percentage")),
						StringBackToMap.KStringVLong(rSet.getString("product")),
						StringBackToMap.KStringVLong(rSet.getString("material")));

				list.add(productionTask);
			}

		}

		HiveUtill.Close(rSet, ps, conn);

		return list;
	}

	public ArrayList<ProductionTask> showAll() throws Exception {
		ArrayList<ProductionTask> list = new ArrayList<ProductionTask>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_task_table";
		PreparedStatement ps = conn.prepareStatement(sql);

		ResultSet rSet = ps.executeQuery();

		while (rSet.next()) {

			ProductionTask productionTask = new ProductionTask(rSet.getString("TaskId"),
					rSet.getString("founder"), rSet.getDate("creationTime"), rSet.getString("chargeMan"),
					rSet.getDate("completionTime"), rSet.getString("taskdescribe"));
			if (Boolean.valueOf(rSet.getString("examine"))) {
				productionTask.setExamine(StringBackToMap.KStringVBool(rSet.getString("percentage")),
						StringBackToMap.KStringVLong(rSet.getString("product")),
						StringBackToMap.KStringVLong(rSet.getString("material")));

			}

				list.add(productionTask);
			}

		
		return list;
		}
	

}
